function start_test() {
    var formData;
    var model_file_path = getCookie("model_file_path");
    if (model_file_path == null) {
        alert("请重新上传训练文件");
        return;
    }
    if (document.getElementById("inputfile").files.length == 0) {
        alert("未选择文件");
    } else {
        formData = new FormData();
        formData.append('file', document.getElementById("inputfile").files[0])
        $("body").mLoading("show");//显示loading组件

        $.ajax({
            url: "./start_predicting",														//后台接收数据地址
            data: formData,
            contentType: 'multipart/form-data',
            type: "POST",
            cache: false,			//上传文件无需缓存
            processData: false,		//用于对data参数进行序列化处理 这里必须false
            contentType: false,
            success: function (response) {
                $("body").mLoading("hide");//隐藏loading组件
                var JsonObj = JSON.parse(response);
                if (JsonObj.status == "success") {
                    alert("测试完成");
                    update_step5_content(JsonObj.result_file_path);
                } else {
                    alert("测试失败," + JsonObj.msg);
                }
            },
            failure: function (response) {
                $("body").mLoading("hide");//隐藏loading组件
                alert("测试失败");
            }
        })

    }

}

function start_train() {
    var train_list = new Array()
    $("#row1 input[type='checkbox']:checked").each(function () {
        train_list.push($(this).val())
    });
    if (train_list.length == 0) {
        alert("未选择训练项")
        return
    }
    var test = "";
    $("#row2 input[type='radio']:checked").each(function () {
        test = $(this).val()
    });
    if (test == "") {
        alert("未选择测试项");
        return
    }
    console.log(train_list.indexOf(test))
    if (train_list.indexOf(test) != -1) {
        alert("不能选择训练列已选择的选项")
        return
    }

    var formData = new FormData();
    formData.append("train_cols", train_list.toString());
    formData.append("test_cols", test);

    $("body").mLoading("show");//显示loading组件

    $.ajax({
        url: '../data_analyse/start_training',
        type: 'POST',
        contentType: 'multipart/form-data',
        cache: false,
        processData: false,// 告诉jQuery不要去处理发送的数据
        contentType: false,// 告诉jQuery不要去设置Content-Type请求头
        data: formData,
        success: function (response) {
            $("body").mLoading("hide");//隐藏loading组件
            var JsonObj = JSON.parse(response);
            if (JsonObj.status == "success") {
                alert("训练完成");
            } else {
                alert("训练失败," + JsonObj["msg"]);
            }
        },
        error: function (data) {
            $("body").mLoading("hide");//隐藏loading组件
            alert("训练失败");
        }
    });
}

function upload_training_flie(event, data, previewId, index) {
    //status : ”success“ / ”failed“      message : "成功" / "格式错误"   colName：[c1，c2，c3，c4]
    // $("body").mLoading("show");//显示loading组件

    var formData;
    if (document.getElementById("f_upload").files.length == 0) {
        alert("未选择文件");
    } else {
        formData = new FormData();
        formData.append('excel_file', document.getElementById("f_upload").files[0]);
        $("body").mLoading("show");//显示loading组件
        console.log(formData.get("excel_file"));
        $.ajax({
            url: "../data_analyse/upload_training_file",	//后台接收数据地址
            data: formData,
            type: "POST",
            cache: false,
            contentType: false,
            processData: false,
            success: function (response) {
                $("body").mLoading("hide");//隐藏loading组件
                var jsonObj = JSON.parse(response);
                if (jsonObj["status"] == "success") {
                    alert("上传成功");
                    update_step3_content(jsonObj["cols"].split(','));
                } else {
                    alert("上传失败," + jsonObj["msg"]);
                }
            },
            failure: function (response) {
                $("body").mLoading("hide");//隐藏loading组件
                alert("上传失败");
            }
        });
    }


}

function update_step3_content(cols) {
    $("#step3").empty();

    $("#step3").append('<h4>第三步：选择训练列与测试列</h4>' +
        '<br><div class="row">' +
        '<div class="col-xs-6 col-sm-3" id="row1"><p>训练列</p></div>' +
        '<div class="col-xs-6 col-sm-3" id="row2"><p>测试列</p></div>' +
        '<div class="row"></div>' +
        '</div><button onclick="window.start_train()" class="btn btn-success" style="margin-right: 20px">开始训练</button>');
    var colName = cols;
    for (var i = 0; i < colName.length; i++) {
        $("#row1").append('<div class="checkbox"><label><input type="checkbox" value="' + colName[i] + '">' + colName[i] + '</label></div>')
    }
    for (var i = 0; i < colName.length; i++) {
        $("#row2").append('<div class="radio"><label><input type="radio" name="optionsRadios" value="' + colName[i] + '">' + colName[i] + '</label></div>')
    }
}

function update_step5_content(response) {
    $("#step5").empty();
    $("#step5").append('<h4>第五步：下载结果</h4><br><a id="download_result_file" href="./result_download" style="font-size: 17px">点击此处下载</a>');
    $("#download_result_file").attr("href", response);
}

function getCookie(name) {
    var prefix = name + "="
    var start = document.cookie.indexOf(prefix)

    if (start == -1) {
        return null;
    }

    var end = document.cookie.indexOf(";", start + prefix.length)
    if (end == -1) {
        end = document.cookie.length;
    }

    var value = document.cookie.substring(start + prefix.length, end)
    return unescape(value);
}
